﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Borton.Beans;

namespace Borton
{

    class BeanNode : TreeNode
    {
        Bean bean;
        public BeanNode(Bean bean)
        {
            this.bean = bean;
        }
        public Bean getBean()
        {
            return bean;
        }
    }
    
    public partial class ApplicationPanel : Form
    {
        Bean[] beans;

        public ApplicationPanel(Bean[] beans)
        {
            this.beans = beans;
            InitializeComponent();
            initToolbar();
            initTabs();
        }

        private void initToolbar()
        {
            ToolStripButton btn = ControlManager.createToolbarButton("Logout");
            btn.Alignment = ToolStripItemAlignment.Right;
            btn.Click += new EventHandler(this.logout_Click);
            applicationToolbar.Items.Add(btn);
        }

        private void logout_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Node_Click(object sender, TreeNodeMouseClickEventArgs e)
        {

            e.Node.Expand();
            showBean(((BeanNode)e.Node).getBean());
        }

        private void initTabs()
        {   
            tableTree.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(this.Node_Click);
            for (int i = 0; i < beans.Length; i++)
            {
                tableTree.Nodes.Add(initBean(beans[i]));
                showBean(beans[i]);
            }
        }

        private BeanNode initBean(Bean bean)
        {
            BeanNode node = new BeanNode(bean);
            node.Text = bean.getTableName();
            node.Name = bean.getTableName() + "Node";
            
            for (int i = 0; i < bean.getReferringBeans().Length; i++)
            {
                node.Nodes.Add(initBean(bean.getReferringBeans()[i]));
            }
            return node;
        }

        private void showBean(Bean bean)
        {
            if (!tabPanel.Controls.ContainsKey(bean.getTableName() + "TabPage"))
            {
                TabPage tabPage = new TabPage();

                tabPage.Name = bean.getTableName() + "TabPage";
                tabPage.Padding = new Padding(1);
                tabPage.Text = bean.getTableName();
                tabPage.UseVisualStyleBackColor = true;


                ToolStripMenuItem closeItem = new ToolStripMenuItem();
                closeItem.Text = "Close";
                closeItem.Click += new EventHandler(closeTab_Click);
                ContextMenuStrip ctxtMenu = new ContextMenuStrip();
                ctxtMenu.Items.Add(closeItem);
                ctxtMenu.Name = bean.getTableName() + "MenuStrip";
                ctxtMenu.Size = new System.Drawing.Size(180, 48);

                tabPage.ContextMenuStrip = ctxtMenu;
                GenericGrid grid = new GenericGrid(bean);

                tabPage.Controls.Add(grid.getPanel());
                tabPanel.Controls.Add(tabPage);
            }
            tabPanel.SelectTab(bean.getTableName() + "TabPage");
        }

        private void closeTab_Click(object sender, EventArgs e)
        {
            tabPanel.SelectedTab.Dispose();
        }
    }
}
